package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sql.DBManagment;
import sql.record.*;

/**
 * Servlet implementation class handleMarked
 */
public class handleMarked extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public handleMarked() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		if (request.getParameter("module").equals("users")) {
			String[] usernames = request.getParameterValues("mark");
			if (usernames != null) {
				List<TableRecord> updates = new LinkedList<TableRecord>();
				for (String s : usernames) {
					User currentUser = new User();
					currentUser.userName = s;
					if (request.getParameter("action").equals("setMarkedAdmin")) {
						try {
							List<User> uList = DBManagment
									.getRecord(currentUser);
							User updateUser = uList.get(0);
							updateUser.type = (updateUser.type == 1) ? 0 : 1;
							updates.add(updateUser);
						} catch (Exception e) {
							out.print("<firefoxsucks>DBfailure</firefoxsucks>");
						}
					} else if (request.getParameter("action").equals(
							"deleteMarked")) {
						updates.add(currentUser);
					}
				}

				try {
					if (request.getParameter("action").equals("setMarkedAdmin")) {
						DBManagment.updateRecord(updates);
					} else if (request.getParameter("action").equals(
							"deleteMarked")) {
						DBManagment.removeRecord(updates);
					}
					out.print("<firefoxsucks>success</firefoxsucks>");
				} catch (Exception e) {
					out.print("<firefoxsucks>failure</firefoxsucks>");
				}
			}
		} else if (request.getParameter("module").equals("mng_courses")){
			String[] courses = request.getParameterValues("mark");
			if (courses != null) {
				List<TableRecord> updates = new LinkedList<TableRecord>();
				for (String s : courses) {
					Course currentCourse = new Course();
					CourseSchedule currentCourseSchedule = new CourseSchedule();
					Registration currentRegistration = new Registration("courseName");
					currentCourse.courseName = s;
					currentCourseSchedule.courseName = s;
					currentRegistration.courseName = s;
					
					if (request.getParameter("action").equals("deleteMarked")) {
						updates.add(currentCourse);
						updates.add(currentCourseSchedule);
						updates.add(currentRegistration);
					}
				}

				try {
					if (request.getParameter("action").equals("deleteMarked")) {
						DBManagment.removeRecord(updates);
					}
					out.print("<firefoxsucks>success</firefoxsucks>");
				} catch (Exception e) {
					out.print("<firefoxsucks>failure</firefoxsucks>");
				}
			}
		} else if (request.getParameter("module").equals("add_courses")){
			String[] courses = request.getParameterValues("mark");
			if (courses != null) {
				List<TableRecord> updates = new LinkedList<TableRecord>();
				for (String s : courses) {
					Registration currentRegistration = new Registration("courseName");
					currentRegistration.courseName = s;
					currentRegistration.userName = (String) request.getSession().getAttribute("loggedUser");
					if (request.getParameter("action").equals("registerMarked")) {
						updates.add(currentRegistration);
					}
				}
				try {
					if (request.getParameter("action").equals("registerMarked")) {
						DBManagment.insertRecord(updates);
					}
					out.print("<firefoxsucks>success</firefoxsucks>");
				} catch (Exception e) {
					out.print("<firefoxsucks>failure</firefoxsucks>");
				}
			}
		}  else if (request.getParameter("module").equals("courses")){
			String[] courses = request.getParameterValues("mark");
			if (courses != null) {
				List<TableRecord> updates = new LinkedList<TableRecord>();
				for (String s : courses) {
					Registration currentRegistration = new Registration("courseName");
					currentRegistration.courseName = s;
					currentRegistration.userName = (String) request.getSession().getAttribute("loggedUser");
					if (request.getParameter("action").equals("unregisterMarked")) {
						updates.add(currentRegistration);
					}
				}
				try {
					if (request.getParameter("action").equals("unregisterMarked")) {
						DBManagment.removeRecord(updates);
					}
					out.print("<firefoxsucks>success</firefoxsucks>");
				} catch (Exception e) {
					out.print("<firefoxsucks>failure</firefoxsucks>");
				}
			}
		}
		out.close();
	}

}
